<?php
/***************************************************************************
 *                             pilotinfo.php
 *                            -------------------
 *                       (APU) Automated Pilot Utility
 *   begin                : Saturday Nov 11, 2006
 *   copyright            : (C) 2004-2006 SF Games
 *   email                : techsupport@sf-games.com
 ****************************************************************************/

	include('include/common.php');
	include('include/config.php');
	include('include/dbcommon.php');
	include('include/randomQuote.php');

	// Snag the pilot ID given
	$pilotID = stripslashes($_GET['id']);

	// Format up the number of decimal places user wants for display of flight hours
	$format = "%2." .$iNumDecimals ."f";

	// Page generation timer start.
	$time = microtime();
	$time = explode(" ", $time);$time = $time[1] + $time[0];$start = $time;

	htmlHeader();
	vaTable($vaName);

	if ( strlen($pilotID) == 0 )
	{
		echo "<p>&nbsp;</p>\n";
		echo "<p>No Pilot ID was given!</p>\n";
		echo "<p>&nbsp;</p>\n";
	}
	else
	{
		//
		// Database Connect
		//

		$db = mysql_connect($dbhost, $dbuname, $dbpass);

		if ( !$db )
		{
			echo "Fatal Error: Could not connect to the SQL server<br>";
			echo "Try again in a few minutes. If problem continues, contact the webmaster of this site.<br>";
		}
		else
		{
			//
			// Pilot Information
			//

			mysql_select_db($dbname);

			$pilotQuery = "SELECT * FROM " .$table_prefix ."PILOTS WHERE PILOT_ID = " ."'" .$pilotID ."'";
			$pilotRec = mysql_query($pilotQuery);
			if ( !$pilotRec )
				die("Query for current pilot record failed.<br>Click your browsers Refresh button to try again.<br>If you continue to receive this error, contact the webmaster<br>Failed with mySQL error: " . mysql_error());
			$pilot = mysql_fetch_array($pilotRec, MYSQL_ASSOC);

			$pirepInfo        = gatherPilotsPireps($pilot["PILOT_ID"]);
			$pilotName        = $pilot["FIRST_NAME"] ." " .$pilot["LAST_NAME"];
			$pilotID          = $pilot["PILOT_ID"];
			$unit             = "Unit Name";
			$rankGfxF         = getRankGraphicFixedWing($pilot["RANK_AC"]);
			$rankGfxFVerbiage = getRankDescriptionFixedWing($pilot["RANK_AC"]);
			$rankGfxH         = floatval($pilot["HOURS_HELO"]) > 0.0 ? getRankGraphicHelicopter($pilot["RANK_HELO"]) : "&nbsp;";
			$rankGfxHVerbiage = floatval($pilot["HOURS_HELO"]) > 0.0 ? getRankDescriptionHelicopter($pilot["RANK_HELO"]) : "&nbsp;";
			$acHrs            = sprintf($format, $pilot["HOURS_AC"]);
			$heloHrs          = sprintf($format, $pilot["HOURS_HELO"]);
			$totalHrs         = sprintf($format, $pilot["TOTAL_HRS"]);
			$numPireps        = $pirepInfo["PIREPS"];
			$numPax           = $pirepInfo["PAX"];
			$numCargo         = $pirepInfo["CARGO"];
			$numFuel          = $pirepInfo["FUEL_BURNED"];
			$joinDate         = $pilot["JOINED"];
			$lastFlyDate      = $pilot["LAST_FLY"];

			if ( $bUseEuroDateFormat )
			{
				$joinDate = SQLDateToShortEuroDate($joinDate);
				$lastFlyDate = SQLDateToShortEuroDate($lastFlyDate);
			}
			else
			{
				$joinDate = SQLDateToShortUSDate($joinDate);
				$lastFlyDate = SQLDateToShortUSDate($lastFlyDate);
			}

			pilotRecord($pilotName, $pilotID, $rankGfxF, $rankGfxFVerbiage, $rankGfxH, $rankGfxHVerbiage, $acHrs, $heloHrs, $totalHrs, $numPireps, $numPax, $numCargo, $numFuel, $joinDate, $lastFlyDate, $pilot["DART"], $pilot["SAR"], $pilot["FLEET_SUP"]);

			//
			// Display all of the pilots PIREP's
			//

			$pirepQuery  = "SELECT * FROM " .$table_prefix ."PIREP WHERE PILOT_ID = '" .$pilotID ."'" ." ORDER BY FLIGHT_DATE";
			$pireps = mysql_query($pirepQuery);
			if ( !$pireps )
				die("Function: gatherPilotsPireps(pilotID). Query for current PIREP reports failed.<br />Click your browsers Refresh button to try again.<br />If you continue to receive this error, contact the webmaster<br>Failed with mySQL error: " . mysql_error());

			beginPilotPirepTable();
			$counter = 0;
			while ($pirepRow = mysql_fetch_array($pireps, MYSQL_ASSOC))
			{
				$counter++;
				addPirepRecord($pirepRow, $counter);
			}
			endPilotPirepTable();
		}
	}

	// End page timing
	$time = microtime();$time = explode(" ", $time);$time = $time[1] + $time[0];
	$finish = $time;$totaltime = ($finish - $start);
	$quote = new RandomQuote;
	printf ("This page %s in %f seconds<br/><br/>", $quote->getQuote(), $totaltime);

	htmlFooter();

?>

<?php

			//////////////////////////////////////////////////
			//                                              //
			//          Roster Support Functions            //
			//                                              //
			//////////////////////////////////////////////////

//
// Display rosters HTML header
//
function htmlHeader()
{
	echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n";
	echo "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n";
	echo "<head>\n";
	echo "<title>APU Pilot Record</title>\n";
	include('include/header.php');
	echo "</head>\n";
	echo "<body>\n";
	echo "<div align=\"center\">\n";
}

//
// Display the rosters HTML footer
//
function htmlFooter()
{
	global $rosterNavigateTo;
	global $rosterNavigateVerbiage;
	global $APU_COPYRIGHT;

	// Display return to another page link/table
	echo "	<table class=\"table_roster_footer\">\n";
	echo "	<tbody>\n";
	echo "		<tr>\n";
	echo "			<td><div align=\"center\"><span class=\"textdark\">Click <a href=\"" .$rosterNavigateTo ."\">here</a> " .$rosterNavigateVerbiage ."</span></div></td>\n";
	echo "		</tr>\n";
	echo "	</tbody>\n";
	echo "	<tfoot><tr><td colspan=\"1\"></td></tr></tfoot>\n";
	echo "	</table>\n";

	// Display Copyright
	echo "<p>" .$APU_COPYRIGHT ."</p>\n";

	// End HTML
	echo "</div>\n";
	echo "</body>\n";
	echo "</html>\n";
}

//
// Display VA name in top table
//
function vaTable($vaName)
{
	echo "	<p>&nbsp;</p>\n";
	echo "	<table class=\"table_roster_header\">\n";
	echo "		<tbody>\n";
	echo "			<tr>\n";
	echo "				<td><div align=\"center\"><span class=\"textredBLrg\">" .$vaName ."</span></div></td>\n";
	echo "			</tr>\n";
	echo "		</tbody>\n";
	echo "		<tfoot><tr><td></td></tr></tfoot>\n";
	echo "	</table>\n";
	echo "	<p>&nbsp;</p>\n";
}

//
// Pilot information table
//
function pilotRecord($pilotName, $pilotID, $rankGfxF, $rankGfxFVerbiage, $rankGfxH, $rankGfxHVerbiage, $acHrs, $heloHrs, $totalHrs, $numPireps, $numPax, $numCargo, $numFuel, $joinDate, $lastFlyDate, $bDart, $bSar, $bFleet)
{
	global $bUseAwards;

	echo "<table class=\"table_pilot_record\">\n";
	echo "<!--DWLayoutTable-->\n";
	echo "	<caption align=\"left\">Pilot Record</caption>\n";
	echo "	<tbody>\n";
	echo "		<tr>\n";
	echo "			<td height=\"23\" colspan=\"2\"><div align=\"center\" class=\"textdarkB\">" .$pilotName ."</div></td>\n";
	echo "			<td colspan=\"2\"><div align=\"center\"><a href=\"pilotemail.php?id=" .$pilotID ."\">" .$pilotID ."</a></div></td>\n";
	echo "			<td colspan=\"2\"><div align=\"center\">Dallas Ft. Worth </div></td>\n";
	echo "		</tr>\n";
	echo "		<tr>\n";
	echo "			<td width=\"77\" height=\"25\" class=\"tableTextCenterB\">Fixed Wing </td>\n";
	echo "			<td colspan=\"2\" class=\"textdarkB\">" .$rankGfxFVerbiage ."</td>\n";
	echo "			<td width=\"57\">" .$rankGfxF ."</td>\n";
	echo "			<td width=\"49\" class=\"textdarkB\">Hrs</td>\n";
	echo "			<td width=\"83\"><div align=\"right\">" .$acHrs ."</div></td>\n";
	echo "		</tr>\n";
	echo "		<tr>\n";
	echo "			<td height=\"25\" class=\"tableTextCenterB\">Helicopter</td>\n";
	echo "			<td colspan=\"2\" class=\"textdarkB\">" .$rankGfxHVerbiage ."</td>\n";
	echo "			<td>" .$rankGfxH ."</td>\n";
	echo "			<td class=\"textdarkB\">Hrs</td>\n";
	echo "			<td><div align=\"right\">" .$heloHrs ."</div></td>\n";
	echo "		</tr>\n";
	echo "		<tr>\n";
	echo "			<td height=\"23\" colspan=\"4\"><!--DWLayoutEmptyCell-->&nbsp;</td>\n";
	echo "			<td class=\"textredB\">Tot Hrs</td>\n";
	echo "			<td class=\"textredB\"><div align=\"right\">" .$totalHrs ."</div></td>\n";
	echo "		</tr>\n";
	echo "		<tr>\n";
	echo "			<td height=\"23\" colspan=\"4\"><!--DWLayoutEmptyCell-->&nbsp;</td>\n";
	echo "			<td class=\"textdarkB\">PIREP</td>\n";
	echo "			<td><div align=\"right\">" .$numPireps ."</div></td>\n";
	echo "		</tr>\n";
	echo "		<tr>\n";
	echo "			<td height=\"23\" colspan=\"4\"><!--DWLayoutEmptyCell-->&nbsp;</td>\n";
	echo "			<td class=\"textdarkB\">PAX</td>\n";
	echo "			<td><div align=\"right\">" .$numPax ."</div></td>\n";
	echo "		</tr>\n";
	echo "		<tr>\n";
	echo "			<td height=\"23\" colspan=\"4\"><!--DWLayoutEmptyCell-->&nbsp;</td>\n";
	echo "			<td class=\"textdarkB\">Cargo</td>\n";
	echo "			<td><div align=\"right\">" .$numCargo ." lbs</div></td>\n";
	echo "		</tr>\n";
	echo "		<tr>\n";
	echo "			<td height=\"23\" colspan=\"4\"><!--DWLayoutEmptyCell-->&nbsp;</td>\n";
	echo "			<td class=\"textdarkB\">Fuel</td>\n";
	echo "			<td><div align=\"right\">" .$numFuel ." lbs</div></td>\n";
	echo "		</tr>\n";
	echo "		<tr>\n";
	echo "			<td height=\"23\" colspan=\"4\"><div align=\"right\" class=\"textdarkB\">Join Date</div></td>\n";
	echo "			<td colspan=\"2\"><div align=\"right\">" .$joinDate ."</div></td>\n";
	echo "		</tr>\n";
	echo "		<tr>\n";
	echo "			<td height=\"23\" colspan=\"4\"><div align=\"right\" class=\"textdarkB\">Last Fly Date</div></td>\n";
	echo "			<td colspan=\"2\"><div align=\"right\">" .$lastFlyDate ."</div></td>\n";
	echo "		</tr>\n";

	if ( $bUseAwards && ($bDart || $bSar || $bFleet) )
	{
		echo "		<tr>\n";
		echo "			<td height=\"23\" colspan=\"6\"><div align=\"center\" class=\"textredB\">Awards<br /><br />";

		if ( $bDart )
			echo "				<img src=\"img/dart.jpg\" alt=\"DART Award\" />";
		if ( $bSar )
			echo "				<img src=\"img/sar.gif\" alt=\"Search and Rescue Award\" />";
		if ( $bFleet )
			echo "				<img src=\"img/fleetsupport.jpg\" alt=\"Fleet Support\" />";

		echo "				</div></td>\n";
		echo "		</tr>\n";
	}

	echo "	</tbody>\n";
	echo "	<tfoot>\n";
	echo "		<tr>\n";
	echo "			<td colspan=\"6\"><span class=\"tableTextLeft\">Click on the Pilot's ID to e-mail this pilot.</span></td>\n";
	echo "		</tr>\n";
	echo "	</tfoot>\n";
	echo "</table>\n";
	echo "<p>&nbsp;</p>\n";
}

//
// Begin the Pilot PIREP table
//
function beginPilotPirepTable()
{
	echo "	<table class=\"table_roster\">\n";
	echo "	<caption>PIREPS</caption>\n";
	echo "		<thead>\n";
	echo "			<tr>\n";
	echo "				<th>#</th><th>Date</th><th>Type</th><th>Tail #</th><th>Flight #</th><th>Dept</th><th>Dest</th><th>Time<br />Out</th><th>Time<br />In</th><th>Flight<br />Time</th><th>Pax</th><th>Cargo</th><th>Comments</th>\n";
	echo "			</tr>\n";
	echo "		</thead>\n";
	echo "	<tbody>\n";
}

//
// Add a PIREP record to the Pilot PIREP Table
//
function addPirepRecord($pirep, $counter)
{
	global $format;
	$flightTime = 0.0;

	$flightTime    = floatval($pirep["TIME_FIXED"]) + floatval($pirep["TIME_HELO"]);
	$totFlightTime = sprintf($format, $flightTime);

	echo "		<tr>\n";
	echo "			<td><a href=\"pirep.php?pirepid=" .$pirep["id"] ."\">" .$counter ."</a></td>\n";
	echo "			<td>" .$pirep["FLIGHT_DATE"] ."</td>\n";
	echo "			<td>" .$pirep["AC_TYPE"] ."</td>\n";
	echo "			<td>" .$pirep["TAIL_NUM"] ."</td>\n";
	echo "			<td class=\"tableTextRight\">" .$pirep["FLIGHT_NUM"] ."</td>\n";
	echo "			<td>" .$pirep["DEPART"] ."</td>\n";
	echo "			<td>" .$pirep["DEST"] ."</td>\n";
	echo "			<td>" .$pirep["TIME_OUT"] ."</td>\n";
	echo "			<td>" .$pirep["TIME_IN"] ."</td>\n";
	echo "			<td class=\"tableTextRight\">" .$totFlightTime ."</td>\n";
	echo "			<td class=\"tableTextRight\">" .$pirep["PAX"] ."</td>\n";
	echo "			<td class=\"tableTextRight\">" .$pirep["CARGO"] ."</td>\n";
	echo "			<td width=\"30%\">" .$pirep["COMMENTS"] ."</td>\n";
	echo "		</tr>\n";
}

//
// End the Pilot PIREP Table
//
function endPilotPirepTable()
{
	echo "		</tbody>\n";
	echo "		<tfoot><tr><td colspan=\"13\"><span class=\"tableTextLeft\">Click on a PIREP ID for editing.</span></td></tr></tfoot>\n";
	echo "	</table>\n";
	echo "<p>&nbsp;</p>\n";
}

?>
